package org.jsola.hr.common;

import jodd.util.StringUtil;
import org.jsola.hr.constant.FileType;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.vo.EmpPaperContractListVO;
import org.jsola.hr.vo.HrContractListVO;
import org.jsola.hr.vo.HrContractVO;

import java.lang.reflect.Field;

/**
 * @author lyh
 */
public class ContractFieldToString {

    /**
     * 合同类型
     */
    private static final String FILE_TYPE = "fileType";

    /**
     * 是否已查看
     */
    private static final String IS_VIEW = "isView";

    /**
     * 剩余有效时间
     */
    private static final String REMAINING_VALID_TIME = "remainingValidTime";

    /**
     * 合同期限
     */
    private static final String DATELINE = "dateline";

    /**
     * 签署状态
     */
    private static final String STATUS = "status";

    /**
     * 小程序是否展示
     */
    private static final String APP_IS_SHOW = "appIsShow";

    /**
     * 空值
     */
    private static final String NULL = "null";

    public static String fieldToString(Field field, HrContractListVO hrContractListVO) {
        //属性名
        String name = field.getName();
        //属性值定义
        Object value;
        //返回值
        String result;
        try {
            value = field.get(hrContractListVO);
        } catch (IllegalAccessException e) {
            throw new HrException("无法访问属性");
        }
        String valueOf = String.valueOf(value);
        if (NULL.equals(valueOf)) {
            return "";
        }
        // 如果属性是婚姻状况
        switch (name) {
            case FILE_TYPE:
                FileType fileType = FileType.find(valueOf);
                if (fileType == null) {
                    return "";
                }
                return fileType.getDesc();
            case IS_VIEW:
                switch (valueOf) {
                    case "true":
                        result = "已查看";
                        break;
                    case "false":
                        result = "未查看";
                        break;
                    default:
                        result = "";
                }
                return result;
            case STATUS:
                switch (valueOf) {
                    case "1":
                        result = "待员工签署";
                        break;
                    case "2":
                        result = "待公司签署";
                        break;
                    case "3":
                        result = "签署完成";
                        break;
                    case "-1":
                        result = "合同过期未签署";
                        break;
                    default:
                        result = "";
                }
                return result;
            case APP_IS_SHOW:
                switch (valueOf) {
                    case "true":
                        result = "是";
                        break;
                    case "false":
                        result = "否";
                        break;
                    default:
                        result = "";
                }
                return result;
            case REMAINING_VALID_TIME:
                if (StringUtil.isNotBlank(valueOf)) {
                    if (valueOf.contains("-")) {
                        return "已到期";
                    }
                    return valueOf + "天";
                }
                return valueOf;
            case DATELINE:
                int month;
                try {
                    month = Integer.parseInt(valueOf);
                } catch (NumberFormatException e) {
                    return valueOf;
                }
                if (month % 12 == 0) {
                    return month / 12 + "年";
                } else {
                    return month + "月";
                }
            default:
                return valueOf;
        }
    }

    /**
     * 纸质合同使用
     */
    public static String fieldToStringEmpPaperContract(Field field, EmpPaperContractListVO empPaperContractListVO) {
        //属性名
        String name = field.getName();
        //属性值定义
        Object value;
        //返回值
        String result;
        try {
            value = field.get(empPaperContractListVO);
        } catch (IllegalAccessException e) {
            throw new HrException("无法访问属性");
        }
        String valueOf = String.valueOf(value);
        if (NULL.equals(valueOf)) {
            return "";
        }
        // 如果属性需要替换
        switch (name) {
            case "type":
                FileType fileType = FileType.find(valueOf);
                if (fileType == null) {
                    return "";
                }
                return fileType.getDesc();
            case STATUS:
                switch (valueOf) {
                    case "1":
                        result = "执行中";
                        break;
                    case "2":
                        result = "已到期";
                        break;
                    case "3":
                        result = "未执行";
                        break;
                    default:
                        result = "其他";
                }
                return result;
            case REMAINING_VALID_TIME:
                if (StringUtil.isNotBlank(valueOf)) {
                    if (valueOf.contains("-")) {
                        return "已到期";
                    }
                    return valueOf + "天";
                }
                return valueOf;
            default:
                return valueOf;
        }
    }

    /**
     * 全部合同使用
     */
    public static String fieldToStringAllContract(Field field, HrContractVO hrContractVO) {
        //属性名
        String name = field.getName();
        //属性值定义
        Object value;
        //返回值
        String result;
        try {
            value = field.get(hrContractVO);
        } catch (IllegalAccessException e) {
            throw new HrException("无法访问属性");
        }
        String valueOf = String.valueOf(value);
        if (NULL.equals(valueOf)) {
            return "";
        }
        // 如果属性需要替换
        switch (name) {
            case "conType":
                FileType fileType = FileType.find(valueOf);
                if (fileType == null) {
                    return "";
                }
                return fileType.getDesc();
            case STATUS:
                switch (valueOf) {
                    case "1":
                        result = "执行中";
                        break;
                    case "2":
                        result = "已到期";
                        break;
                    case "3":
                        result = "未执行";
                        break;
                    default:
                        result = "其他";
                }
                return result;
            case REMAINING_VALID_TIME:
                if (StringUtil.isNotBlank(valueOf)) {
                    if (valueOf.contains("-")) {
                        return "已到期";
                    }
                    return valueOf + "天";
                }
                return valueOf;
            case "isEntity":
                switch (valueOf) {
                    case "true":
                        result = "纸质合同";
                        break;
                    case "false":
                        result = "电子合同";
                        break;
                    default:
                        result = "";
                }
                return result;
            default:
                return valueOf;
        }
    }


}
